# Launching nkAstraeus

The aim behind nkAstraeus is to articulate all other components to provide a full engine, ready to use. As such, initializing this component will initialize all components it is using, in one-go.

In this tutorial we will see how to initialize, and what this brings once it is done !

## Lifetime of the system, launch and shutdown

Like any other component, what is needed with nkAstraeus is to set it up through its dedicated function, and shut it down once we're done. Let's see how we can do that. First, include :

#include <NilkinsAstraeus/System.h>

Then, we can start calling the functions we need :

if (!nkAstraeus::System::getInstance()->initialize()) return -1 ;

With this, the internal components will get initialized as they require. If it returns true, the component is ready to be used !

The System class initializes all sub-systems, currently the GraphicsEngine and the ScriptsEngine. Ultimately, they :

- Initialize nkGraphics with default settings
- Initialize an environment nkScripts, and prepare it to use the nkEngine API

As such, once this operation is done, it is possible to talk directly to the nkGraphics or nkScripts API. In fact, nkAstraeus is thought as being a component automating some operations, offering a higher level API where it matters. It isn't meant to replace the other APIs, but rather to complete them. For instance, the material system offers out-of-the-box shaders, hidden behind an API for ease of use. But it's not meant to replace the approach taken in nkGraphics about programs and shaders.

So for instance, what we can do is now launch a nkGraphics context right away :

nkGraphics::RenderContext* context = nkGraphics::RenderContextManager::getInstance()->createRenderContext(nkGraphics::RenderContextDescriptor(800, 600, false, true)) ; nkGraphics::System::getInstance()->run(context) ;

And we get an empty window with the default painting.

Alternatively, we could use the scriting wrapping of the system to offer a dynamic environment to manipulate it :

#include <NilkinsAstraeus/Scripts/ScriptsEngine.h> ... ... nkScripts::Script* script = nkScripts::ScriptManager::getInstance()->createOrRetrieve("script") ; script->setTargetInterpreter(nkScripts::INTERPRETER::LUA) ; script->setSources ( R"eos( -- Address the C++ API from Lua -- Create two textures local tex = nkGraphics.TextureManager.get("environment") ; local irr = nkGraphics.TextureManager.createOrRetrieve("irradiance") ; -- Compute the irradiance map and load it nkGraphics.TextureUtils.computeIrradianceSH(tex, irr) ; irr:load() ; -- And save it, for instance nkGraphics.TextureSaver.saveToHdr(irr, "Textures/environmentIrradiance.hdr") ; )eos" ) ; script->load() ; nkAstraeus::System::getInstance()->getScriptsEngine()->getEnvironment()->execute(*script) ;

The way to retrieve the dedicated scripting environment is through the ScriptsEngine class, which is the engine part dealing with nkScripts. The scripting environment within nkAstraeus offers a wrap of the nkEngine API, enabling use of the C++ API within the scripting environment.
Here, we are using that to compute the irradiance map of an environment texture and save it on disk. For more information about what can be done and how, please see the API documentation about the nkAstraeus::lua namespace, and all the wrappers inside.

Specific parts about nkScripts and nkGraphics won't be detailed here. For more guidance on them, please take a look at the dedicated tutorial sections.

So now that we had fun, time to shutdown everything. This is easily done through :

nkAstraeus::System::getInstance()->shutdown() ;

Through this only call, the nkGraphics component will be shutdown, and the nkScripts environment will be cleaned. So, like other components, the process is :

1. Initialize the component
2. Have fun
3. Shutdown once we're done

This will ensure optimal functioning of the component itself.

## About logging

Logging is an important part of the component as all related information about warnings or errors will be logged. To be able to get all this information, the nkAstreaus component does it like any other : use nkLog as a basis.
First, let's go over the includes required :

#include <NilkinsAstraeus/Log/LogManager.h> #include <NilkinsLog/Loggers/ConsoleLogger.h> #include <memory>

This will allow us to fully setup the capability. Note that we're using the built-in ConsoleLogger in this example, so the console needs to be visible to fully enjoy it.

std::unique_ptr<nkLog::Logger> logger = std::make_unique<nkLog::ConsoleLogger>() ; nkAstraeus::LogManager::getInstance()->setReceiver(logger.get()) ;

We create a logger, using unique_ptr for ease of use. Then, we request nkAstraeus' LogManager to use it as its log receiver. Now, whenever something has to be logged from nkAstraeus, the console will be the place to look for.

Note that currently, the LogManager within nkAstraeus does not listen to other components logs. This means that if you require logging from nkGraphics, or nkScripts, you will need to also register the receiver within their own LogManager.

## Conclusion

To conclude now we know how to launch nkAstraeus. Through the engine initialization and shutdown, we learned how to manage its lifetime. Thanks to the different abstractions made inside, we also have more information about what nkAstraeus is supposed to do for us.

Next tutorials will dig more into specific points within the component. Stay tuned as there's more to come !